package com.jjb.www.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeTextView;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.AetNumberFillInfoApi;
import com.jjb.www.http.api.AetNumberFillInfoV2Api;
import com.jjb.www.http.api.AnswerApi;
import com.jjb.www.http.api.AnswerV2Api;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.ui.activity.SequentialPracticeActivity;
import com.jjb.www.ui.adapter.ComprehensiveAdapter;
import com.jjb.www.ui.adapter.ComprehensiveV2Adapter;
import com.jjb.www.utils.StringBufferUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Android CRG
 * time   : 2018/10/18
 * desc   : 综合题Fragment
 */
public final class ComprehensiveFragment extends AppFragment<SequentialPracticeActivity> {
    private static final String INTENT_KEY_ORDER_NUMBER = "order_number";
    private static final String INTENT_KEY_PAPERID = "paperid";
    private static final String INTENT_KEY_NUMBER_SCORE = "number_score";
    private static final String INTENT_KEY_NUMBER = "number";
    private static final String INTENT_KEY_EXAM_TYPE = "exam_type";
    private static final String INTENT_KEY_FROM = "fromType";
    private ShapeTextView mTvNumber;
    private ShapeLinearLayout mRvQuestion;
    private ShapeTextView mTvTitle;
    private WrapRecyclerView mRvSelect;
    private ComprehensiveAdapter adapter;
    private ComprehensiveV2Adapter adapter2;

    private OnSelectListener mListener;
    //当前题的答案排序
    private String sort;
    //当前题的 试卷试题id
    private Integer fillId;
    private List<String> sortList;
    //当前答题的序号
    private String orderNumber;
    //列表数据
    private List<AetNumberFillInfoApi.Bean.QuestionSonListDTO> list;

    //列表数据
    private List<AetNumberFillInfoV2Api.Bean.QuestionSonListDTO> list2;

    public static ComprehensiveFragment newInstance(String fromType, Integer order_number, Integer paperid, Integer number_score, Integer number, Integer exam_type, OnSelectListener listener) {
        ComprehensiveFragment fragment = new ComprehensiveFragment();
        fragment.setOnSelectListener(listener);
        Bundle bundle = new Bundle();
        bundle.putInt(INTENT_KEY_ORDER_NUMBER, order_number);
        bundle.putInt(INTENT_KEY_PAPERID, paperid);
        bundle.putInt(INTENT_KEY_NUMBER_SCORE, number_score);
        bundle.putInt(INTENT_KEY_NUMBER, number);
        bundle.putInt(INTENT_KEY_EXAM_TYPE, exam_type);
        bundle.putString(INTENT_KEY_FROM, fromType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_comprehensive;
    }

    @Override
    protected void initView() {
        mTvNumber = findViewById(R.id.tv_number);
        mRvQuestion = findViewById(R.id.rv_question);
        mTvTitle = findViewById(R.id.tv_title);
        mRvSelect = findViewById(R.id.rv_select);
        mRvSelect.setLayoutManager(new LinearLayoutManager(getContext()));

        if (getString("fromType").equals("ceping")) {
            adapter2 = new ComprehensiveV2Adapter(getContext(), new ComprehensiveV2Adapter.OnChildListener() {
                @Override
                public void selectNumber(int fatherPersion, int persion) {
                    list2 = new ArrayList<>();
                    list2.clear();
                    list2 = adapter2.getData();
                    for (int i = 0; i < list2.size(); i++) {
                        if (i == fatherPersion) {
                            //试题类型  3多选
                            fillId = list2.get(fatherPersion).getFill_id();
                            if (list2.get(i).getType() == 3) {
                                for (int a = 0; a < list2.get(i).getQuestion_answer().size(); a++) {
                                    if (a == persion) {
                                        if (list2.get(i).getQuestion_answer().get(a).isSelect()) {
                                            list2.get(i).getQuestion_answer().get(a).setSelect(false);
                                        } else {
                                            list2.get(i).getQuestion_answer().get(a).setSelect(true);
                                        }

                                    }
                                }
                                sortList = new ArrayList<>();
                                sortList.clear();
                                for (AetNumberFillInfoV2Api.Bean.QuestionSonListDTO.QuestionAnswerDTO bean : list2.get(i).getQuestion_answer()) {
                                    if (bean.isSelect()) {
                                        sortList.add(bean.getSort() + "");
                                    }
                                }
                                sort = StringBufferUtils.arrToStr(sortList, ",");
                                answerV2(fillId);
                            } else {
                                //试题类型  1单选 2判断
                                for (int a = 0; a < list2.get(i).getQuestion_answer().size(); a++) {
                                    if (a == persion) {
                                        list2.get(i).getQuestion_answer().get(persion).setSelect(true);
                                    } else {
                                        list2.get(i).getQuestion_answer().get(a).setSelect(false);
                                    }
                                }

                                for (AetNumberFillInfoV2Api.Bean.QuestionSonListDTO.QuestionAnswerDTO bean : list2.get(i).getQuestion_answer()) {
                                    if (bean.isSelect()) {
                                        sort = bean.getSort() + "";
                                    }
                                }
                                answerV2(fillId);
                            }

                        }
                    }
                    adapter2.setData(list2);
                }
            });
            mRvSelect.setAdapter(adapter2);
        } else {
            adapter = new ComprehensiveAdapter(getContext(), new ComprehensiveAdapter.OnChildListener() {
                @Override
                public void selectNumber(int fatherPersion, int persion) {
                    list = new ArrayList<>();
                    list.clear();
                    list = adapter.getData();
                    for (int i = 0; i < list.size(); i++) {
                        if (i == fatherPersion) {
                            //试题类型  3多选
                            fillId = list.get(fatherPersion).getFill_id();
                            if (list.get(i).getType() == 3) {
                                for (int a = 0; a < list.get(i).getQuestion_answer().size(); a++) {
                                    if (a == persion) {
                                        if (list.get(i).getQuestion_answer().get(a).isSelect()) {
                                            list.get(i).getQuestion_answer().get(a).setSelect(false);
                                        } else {
                                            list.get(i).getQuestion_answer().get(a).setSelect(true);
                                        }

                                    }
                                }
                                sortList = new ArrayList<>();
                                sortList.clear();
                                for (AetNumberFillInfoApi.Bean.QuestionSonListDTO.QuestionAnswerDTO bean : list.get(i).getQuestion_answer()) {
                                    if (bean.isSelect()) {
                                        sortList.add(bean.getSort() + "");
                                    }
                                }
                                sort = StringBufferUtils.arrToStr(sortList, ",");
                                answer(fillId);
                            } else {
                                //试题类型  1单选 2判断
                                for (int a = 0; a < list.get(i).getQuestion_answer().size(); a++) {
                                    if (a == persion) {
                                        list.get(i).getQuestion_answer().get(persion).setSelect(true);
                                    } else {
                                        list.get(i).getQuestion_answer().get(a).setSelect(false);
                                    }
                                }

                                for (AetNumberFillInfoApi.Bean.QuestionSonListDTO.QuestionAnswerDTO bean : list.get(i).getQuestion_answer()) {
                                    if (bean.isSelect()) {
                                        sort = bean.getSort() + "";
                                    }
                                }
                                answer(fillId);
                            }

                        }
                    }
                    adapter.setData(list);
                }
            });
            mRvSelect.setAdapter(adapter);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (getString("fromType").equals("ceping")){
            getV2Paper();
        }else {
            getPaper();
        }
    }

    public void setOnSelectListener(OnSelectListener listener) {
        this.mListener = listener;
    }
    private void getV2Paper() {
        EasyHttp.get(this)
                .api(new AetNumberFillInfoV2Api()
                        .setOrder_number(getInt(INTENT_KEY_ORDER_NUMBER) + "")
                        .setPagerId(getInt(INTENT_KEY_PAPERID) + "")
                )
                .request(new HttpCallback<HttpData<AetNumberFillInfoV2Api.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AetNumberFillInfoV2Api.Bean> data) {
                        mListener.questionDetailsV2(data.getData());
                        mRvQuestion.setVisibility(View.VISIBLE);
                        if (data.getData().getIs_answer() == 1) {

                            for (AetNumberFillInfoV2Api.Bean.QuestionSonListDTO bean : data.getData().getQuestion_son_list()) {
                                String[] temp = null;
                                temp = bean.getAnswer_sort().split(",");
                                for (AetNumberFillInfoV2Api.Bean.QuestionSonListDTO.QuestionAnswerDTO bean2 : bean.getQuestion_answer()) {
                                    for (String str : temp) {
                                        if (bean2.getSort() == Integer.parseInt(str)) {
                                            bean2.setSelect(true);
                                        }
                                    }
                                }

                            }
                        }
                        adapter2.setData(data.getData().getQuestion_son_list());
                        orderNumber = data.getData().getOrder_number() + "";
                        mTvNumber.setText("(" + getInt(INTENT_KEY_NUMBER) + "/1，每题" + getInt(INTENT_KEY_NUMBER_SCORE) + "分)");
                        mTvTitle.setText(data.getData().getOrder_number() + "、" + data.getData().getBackground_title());
                    }
                });
    }
    /**
     * 答题
     *
     * @param fill_id 试卷试题id
     */
    private void answer(Integer fill_id) {
        EasyHttp.post(this)
                .api(new AnswerApi()
                        .setOrder_number(orderNumber)
                        .setPaper_id(getInt(INTENT_KEY_PAPERID) + "")
                        .setExam_type(getInt(INTENT_KEY_EXAM_TYPE) + "")
                        .setSort(sort)
                        .setFill_id(fill_id + "")
                )
                .request(new HttpCallback<HttpData<AnswerApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AnswerApi.Bean> data) {

                    }
                });
    }
    /**
     * 答题
     *
     * @param fill_id 试卷试题id
     */
    private void answerV2(Integer fill_id) {
        EasyHttp.post(this)
                .api(new AnswerV2Api()
                        .setOrder_number(orderNumber)
                        .setPaper_id(getInt(INTENT_KEY_PAPERID) + "")
                        .setExam_type(getInt(INTENT_KEY_EXAM_TYPE) + "")
                        .setSort(sort)
                        .setFill_id(fill_id + "")
                )
                .request(new HttpCallback<HttpData<AnswerV2Api.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AnswerV2Api.Bean> data) {

                    }
                });
    }
    /**
     * 获取某道题的详情
     */
    private void getPaper() {
        EasyHttp.get(this)
                .api(new AetNumberFillInfoApi()
                        .setOrder_number(getInt(INTENT_KEY_ORDER_NUMBER) + "")
                        .setPagerId(getInt(INTENT_KEY_PAPERID) + "")
                )
                .request(new HttpCallback<HttpData<AetNumberFillInfoApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<AetNumberFillInfoApi.Bean> data) {
                        mListener.questionDetails(data.getData());
                        mRvQuestion.setVisibility(View.VISIBLE);
                        if (data.getData().getIs_answer() == 1) {

                            for (AetNumberFillInfoApi.Bean.QuestionSonListDTO bean : data.getData().getQuestion_son_list()) {
                                String[] temp = null;
                                temp = bean.getAnswer_sort().split(",");
                                for (AetNumberFillInfoApi.Bean.QuestionSonListDTO.QuestionAnswerDTO bean2 : bean.getQuestion_answer()) {
                                    for (String str : temp) {
                                        if (bean2.getSort() == Integer.parseInt(str)) {
                                            bean2.setSelect(true);
                                        }
                                    }
                                }

                            }
                        }
                        adapter.setData(data.getData().getQuestion_son_list());
                        orderNumber = data.getData().getOrder_number() + "";
                        mTvNumber.setText("(" + getInt(INTENT_KEY_NUMBER) + "/1，每题" + getInt(INTENT_KEY_NUMBER_SCORE) + "分)");
                        mTvTitle.setText(data.getData().getOrder_number() + "、" + data.getData().getBackground_title());
                    }
                });
    }

    public interface OnSelectListener {
        /**
         * 题详情
         *
         * @param bean
         */
        void questionDetails(AetNumberFillInfoApi.Bean bean);

        //题详情
        void questionDetailsV2(AetNumberFillInfoV2Api.Bean bean);
    }
}